rm(list=ls())
load("../Data/revealed.RData")
set.seed(873209)
adapt <- 1e4
burn <- 25e4
samp <- 1e4
thin <- 25
chains <- 3

group.counter <- unique(allmeps$party)
group <- rep(NA,nrow(allmeps))
for (i in 1:length(group.counter)){
  group<- ifelse(allmeps$party==group.counter[i],i,group)
}
n.groups <- length(levels(as.factor(allmeps$party)))

nat.counter <- unique(allmeps$state)
nat <- rep(NA,nrow(allmeps))
for (i in 1:length(nat.counter)){
  nat<- ifelse(allmeps$state==nat.counter[i],i,nat)
}
n.nat <- length(levels(as.factor(allmeps$state)))

year <- allmeps$ep -3
n.year <- length(unique(year))


X <- with(allmeps,cbind(natCLPR,euCLPR,euCLPR_STV,natSemi_OLPR,
                        euSemi_OLPR,natSTV,euSTV,euOLPR,natSMP,epback,natback,
                        other,age,groupRole,committeeRole))
n.betas <- ncol(X)
Jagsdata <- list(r=allmeps$votes,n=allmeps$totalvotes,n.obs=nrow(allmeps),
                 X=X,group=group,n.groups=n.groups,n.nat=n.nat,nat=nat,
                 year=year,n.year=n.year,n.betas=n.betas,
                 b0=rep(0,n.betas),B0=diag(.001,n.betas))

initsfunction <- function(chain) return(switch(chain,
                                               "1"=list(deltaTmp=rnorm(n.groups,0,2),gammaTmp=rnorm(n.nat,1,2),beta=rnorm(n.betas,0,3),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Super-Duper",.RNG.seed=2),
                                               "2"=list(deltaTmp=rnorm(n.groups,-1,3),gammaTmp=rnorm(n.nat,-2,3),beta=rnorm(n.betas,-1,2),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Wichmann-Hill",.RNG.seed=2),
                                               "3"=list(deltaTmp=rnorm(n.groups,1,2),gammaTmp=rnorm(n.nat,0,3),beta=rnorm(n.betas,1,1.5),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Mersenne-Twister",.RNG.seed=2)))

foo <- run.jags(model="../jags/binomialhierSystem.jags",
                data=Jagsdata,inits=initsfunction,modules="glm",
                adapt=adapt,burnin=burn,sample=samp,
                n.chains=chains,method='parallel',thin=thin,#summarise=FALSE,plots=FALSE,
                monitor=c("beta","gamma","deviance","delta","nu"))

save(foo,file="../Posteriors/ResultsVotesSystem.RData")

# Debates  only for EP5 - EP7
debmeps <- subset(allmeps,subset=allmeps$ep>4)
group.counter <- unique(debmeps$party)
group <- rep(NA,nrow(debmeps))
for (i in 1:length(group.counter)){
  group<- ifelse(debmeps$party==group.counter[i],i,group)
}
n.groups <- length(levels(as.factor(debmeps$party)))

nat.counter <- unique(debmeps$state)
nat <- rep(NA,nrow(debmeps))
for (i in 1:length(nat.counter)){
  nat<- ifelse(debmeps$state==nat.counter[i],i,nat)
}
n.nat <- length(levels(as.factor(debmeps$state)))

year <- debmeps$ep -4
n.year <- length(unique(year))

chains <- 3

X <- with(debmeps,cbind(natCLPR,euCLPR,euCLPR_STV,natSemi_OLPR,
                        euSemi_OLPR,natSTV,euSTV,euOLPR,natSMP,epback,natback,
                        other,age,groupRole,committeeRole))
n.betas <- ncol(X)
Jagsdata <- list(r=debmeps$speeches,n=debmeps$debates,n.obs=nrow(debmeps),
                 X=X,group=group,n.groups=n.groups,n.nat=n.nat,nat=nat,
                 year=year,n.year=n.year,n.betas=n.betas,
                 b0=rep(0,n.betas),B0=diag(.001,n.betas))
initsfunction <- function(chain) return(switch(chain,
                                               "1"=list(deltaTmp=rnorm(n.groups,0,2),gammaTmp=rnorm(n.nat,1,2),beta=rnorm(n.betas,0,3),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Super-Duper",.RNG.seed=2),
                                               "2"=list(deltaTmp=rnorm(n.groups,-1,3),gammaTmp=rnorm(n.nat,-2,3),beta=rnorm(n.betas,-1,2),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Wichmann-Hill",.RNG.seed=2),
                                               "3"=list(deltaTmp=rnorm(n.groups,1,2),gammaTmp=rnorm(n.nat,0,3),beta=rnorm(n.betas,1,1.5),
                                                        nuTmp=rnorm(n.year),
                                                        .RNG.name="base::Mersenne-Twister",.RNG.seed=2)))

fooDebates <- run.jags(model="../jags/binomialhierSystem.jags",
                data=Jagsdata,inits=initsfunction,modules="glm",
                adapt=adapt,burnin=burn,sample=samp,
                n.chains=chains,method='parallel',thin=thin,#summarise=FALSE,plots=FALSE,
                monitor=c("beta","gamma","deviance","delta","nu"))
save(fooDebates,file="../Posteriors/ResultsDebatesSystem.RData")
